#include<iostream>
#include<string>

using namespace std;

class Solution {
public:
	int myAtoi(string str)
	{
		string num;
		char ch = '+';
		int flag = 0;
		for (int i = 0; i < str.size(); i++)
		{
			if (str[i] == ' ')
			{
				if (num.size() == 0 && flag == 0)continue;
				else break;
			}

			else if ((str[i] == '+' || str[i] == '-') && flag == 0)
			{
				ch = str[i];
				flag = 1;
			}


			else if (str[i] >= '0' && str[i] <= '9')
			{
				num += str[i];
				flag = 1;
			}

			else
			{
				break;
			}

		}
		if (num.size() == 0)return 0;
		reverse(num.begin(), num.end());
		int tmp = 0;
		if (ch == '+')
		{

			for (int i = 0; i < num.size(); i++)
			{
				if ((tmp + (num[i] - '0') * pow(10, i)) > pow(2, 31) - 1)
				{
					return pow(2, 31) - 1;
				}
				tmp += (num[i] - '0') * pow(10, i);
			}
			return tmp;
		}
		else
		{
			for (int i = 0; i < num.size(); i++)
			{
				if ((tmp - (num[i] - '0') * pow(10, i)) < -pow(2, 31))
				{
					return -pow(2, 31);
				}
				tmp -= (num[i] - '0') * pow(10, i);
			}
			return tmp;
		}

	}
};

int main()
{
	string s1("02 212213");
	string s2("saa 3432 ss");
	string s3("0-1");
	string s4("+323 ");
	string s5("    -  11");
	string s6("  -23323");
	Solution x;
	int num1=x.myAtoi(s1);
	int num2=x.myAtoi(s2);
	int num3=x.myAtoi(s3);
	int num4=x.myAtoi(s4);
	int num5=x.myAtoi(s5);
	int num6=x.myAtoi(s6);
	cout << num1 << endl;
	cout << num2 << endl;
	cout << num3 << endl;
	cout << num4 << endl;
	cout << num5 << endl;
	cout << num6 << endl;
	return 0;
}